home *** CD-ROM | disk | FTP | other *** search
- Path: gryphon.phoenix.net!usenet
- From: brucew@phoenix.net (Bruce Wedding)
- Newsgroups: comp.lang.c,comp.lang.c++,alt.msdos.programmer
- Subject: Re: Clearing the keyboard buffer
- Date: Tue, 23 Jan 1996 00:43:22 GMT
- Organization: BranPaul Systems
- Message-ID: <4e18m5$on1@gryphon.phoenix.net>
- References: <4dtqi4$60u@cdc2.cdc.net>
- NNTP-Posting-Host: dial120.phoenix.net
- X-Newsreader: Moe's Newsreader
-
- mart@vianet.on.ca (Mart) wrote:
-
- > I'm using Turbo C++ v. 3.1 and I need to be able to clear the keyboard
- >buffer.
-
- I'll probably be flamed for mentioning this, but I'm going to anyway.
- This is not STANDARD, in fact according to the ANSI Standard, it
- results in undefined behavior. That ought to satisfy the pedants.
-
- fflush(stdin); /* will flush the keyboard in Turbo C */
-
- Here is another version from Snippets. It includes a replacement
- kbhit that is 10s if not 100s of times faster than Borlands.
-
- /*
- ** by David Goodenough & Bob Stout
- */
-
- #include <stdio.h>
- #include <dos.h>
- #include "extkword.h"
- #include "ext_keys.h"
- #include "mk_fp.h"
-
- #define biosseg 0x40
-
- #define HEAD (*((unsigned short FAR *)MK_FP(biosseg, 0x1a)))
- #define TAIL (*((unsigned short FAR *)MK_FP(biosseg, 0x1c)))
-
- /*
- ** Detect a pending keypress
- */
-
- int fast_kbhit(void)
- {
- return HEAD - TAIL;
- }
-
- /*
- ** Clear the keyboard buffer
- */
-
- void fast_kbflush(void)
- {
- HEAD = TAIL;
- }
-
- /*
- ** Enhanced work-alike for BASIC's INKEY$ function
- */
-
- int ext_inkey(void)
- {
- if (!fast_kbhit())
- return EOF;
- else return ext_getch();
- }
-
- #ifdef TEST
-
- #include <conio.h>
- #include <time.h>
-
- main()
- {
- clock_t Wait;
- int key;
-
- puts("Hit some keys while I kill some time...");
- Wait = clock();
- while (2 > ((clock() - Wait) / CLK_TCK))
- ;
-
- puts("OK, stop hitting keys while I flush the keyboard...");
- Wait = clock();
- while (2 > ((clock() - Wait) / CLK_TCK))
- ;
-
- fast_kbflush();
- puts("Optionally, hit some key you didn't hit before...");
- Wait = clock();
- while (2 > ((clock() - Wait) / CLK_TCK))
- ;
- if (EOF == (key = ext_inkey()))
- puts("You didn't hit anything");
- else printf("You hit extended keycode 0x%04X\n", key);
-
- puts("OK, now hit some other key you didn't hit before...");
-
- while (!fast_kbhit())
- ;
- printf("You hit extended keycode 0x%04X\n", ext_getch());
- return 0;
- }
-
- #endif /* TEST */
-
- Bruce D. Wedding Have Compiler, Will Travel!
- Perspicacious Programming Performed Promptly
- Katy, Texas, USA, Planet Earth, Milkyway Galaxy, Known Universe
-
-